package cn.miwu52.homeworkerp.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;

import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.miwu52.homeworkerp.Util.MySqlOpenHelper;
import cn.miwu52.homeworkerp.bean.User;

public class UserDao {
    private SQLiteDatabase db;
    private MySqlOpenHelper mySqlOpenHelper;
    public UserDao(Context context) {

        try {
            System.out.println("版本号" + context.getPackageName() + context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
            mySqlOpenHelper = new MySqlOpenHelper(context, "erp", null, context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        db = mySqlOpenHelper.getWritableDatabase();
    }

    /**
     * 判断当前用户名是否已注册，已注册返回假，未注册返回真
     * @param username
     * @return
     */
    public boolean findUserByUserName(String username) {
        Cursor user = db.query("User", null, "username='" + username + "'", null, null, null, null);

        if (user.getCount() > 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * @param username
     * @return
     */
    @SuppressLint("Range")
    public String findUserGrade(String username) {
        Cursor user = db.query("User", null, "username='" + username + "'", null, null, null, null);
        while (user.moveToNext()) {
            return user.getString(user.getColumnIndex("grade"));
        }
        return user.getString(user.getColumnIndex("grade"));
    }

    /**
     * 注册用户
     *
     * @param username 用户名
     * @param password 用户密码
     * @param grade    用户等级
     */
    public boolean insertUserName(String username, String password, String grade) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("username", username);
        contentValues.put("password", password);
        Date date = new Date();
        contentValues.put("regtime", date.toString());
        contentValues.put("grade", grade);
        long user = db.insert("User", null, contentValues);
        if (user > 0) {
            return true;
        } else {
            return false;
        }
    }

    public void updateLoginTimeByUserName(String username) {
        ContentValues contentValues = new ContentValues();
        Date date = new Date();
        contentValues.put("logintime", date.toString());
        int user = db.update("User", contentValues, "username=?", new String[]{username});
    }

    /**
     * 登录方法
     *
     * @param username
     * @param password
     * @return
     */
    @SuppressLint("Range")
    public boolean authByUserNameAndPassWord(String username, String password) {
        Cursor user = db.query("User", null, "username='" + username + "'", null, null, null, null);
        while (user.moveToNext()) {
            if (user.getString(user.getColumnIndex("password")).equals(password)) {
                updateLoginTimeByUserName(username);
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 获取用户信息
     *
     * @param username
     * @return
     */
    @SuppressLint("Range")
    public User getUserInfoByName(String username) {
        Cursor user = db.query("User", null, "username='" + username + "'", null, null, null, null);
        User user1;
        while (user.moveToNext()) {
            user1 = new User();
            user1.setId(user.getInt(user.getColumnIndex("id")));
            user1.setUserName(user.getString(user.getColumnIndex("username")));
            user1.setPassWord(user.getString(user.getColumnIndex("password")));
            user1.setPhone(user.getString(user.getColumnIndex("phone")));
            user1.setTotalInEntrepot(user.getInt(user.getColumnIndex("totalInEntrepot")));
            user1.setTotalOutEntrepot(user.getInt(user.getColumnIndex("totalOutEntrepot")));
            //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.fffffffff");
            //user1.setRegTime(simpleDateFormat.parse(user.getString(user.getColumnIndex("regtime"))));
            //user1.setLoginTime(simpleDateFormat.parse(user.getString(user.getColumnIndex("logintime"))));
            user1.setGrade(user.getString(user.getColumnIndex("grade")));
            return user1;
        }
        return null;
    }

    /**
     * 增加入库
     *
     * @param username
     */
    public void updateInEntrepotSum(String username) {
        User userInfoByName = getUserInfoByName(username);
        ContentValues contentValues = new ContentValues();
        contentValues.put("totalInEntrepot", userInfoByName.getTotalInEntrepot() + 1);
        db.update("User", contentValues, "username=?", new String[]{username});
    }

    /**
     * 增加出库
     *
     * @param username
     */
    public void updateOutEntrepotSum(String username) {
        User userInfoByName = getUserInfoByName(username);
        ContentValues contentValues = new ContentValues();
        contentValues.put("totalOutEntrepot", userInfoByName.getTotalOutEntrepot() + 1);
        db.update("User", contentValues, "username=?", new String[]{username});
    }

    /**
     * 查询用户密码  用于修改密码时鉴权
     *
     * @param username 用户名
     * @return 返回用户密码
     */
    @SuppressLint("Range")
    public String findPasswordByUserName(String username) {
        Cursor user = db.query("User", null, "username=?", new String[]{username}, null, null, null);
        while (user.moveToNext()) {
            return user.getString(user.getColumnIndex("password"));
        }
        return null;
    }

    /**
     * 根据用户名修改密码
     *
     * @param username 用户名
     * @param password 新密码
     * @return 修改成功返回真，修改失败返回假
     */
    public boolean updatePasswordByUserName(String username, String password) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("password", password);
        int user = db.update("User", contentValues, "username=?", new String[]{username});
        if (user > 0) {
            return true;
        }
        return false;
    }
}
